package com.hoow.shakoow.ai.components.imageView

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import com.hoow.shakoow.R
import com.hoow.shakoow.ai.utils.DisplayUtils

class AiRoundImageView : AppCompatImageView {
    private var rightBottomRadius: Int
    private var leftBottomRadius: Int
    private var rightTopRadius: Int
    private var leftTopRadius: Int
    private var radius: Int = 4
    private val defaultRadius: Int = 4

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int)
            : super(context, attributeSet, defStyleAttr) {
        val typeArray = context.obtainStyledAttributes(attributeSet, R.styleable.AiRoundImageView)
        radius = DisplayUtils.dp2px(
            context,
            typeArray.getInt(R.styleable.AiRoundImageView_aiRadius, defaultRadius) * 1F
        )

        leftTopRadius = DisplayUtils.dp2px(
            context, typeArray.getInt(
                R.styleable.AiRoundImageView_aiLeftTopRadius,
                defaultRadius
            ) * 1F
        )

        rightTopRadius = DisplayUtils.dp2px(
            context, typeArray.getInt(
                R.styleable.AiRoundImageView_aiRightTopRadius,
                defaultRadius
            ) * 1F
        )

        leftBottomRadius = DisplayUtils.dp2px(
            context, typeArray.getInt(
                R.styleable.AiRoundImageView_aiLeftBottomRadius,
                defaultRadius
            ) * 1F
        )

        rightBottomRadius = DisplayUtils.dp2px(
            context, typeArray.getInt(
                R.styleable.AiRoundImageView_aiRightBottomRadius,
                defaultRadius
            ) * 1F
        )

        if (radius != 0) {
            if (leftTopRadius == 0) {
                leftTopRadius = radius
            }
            if (rightTopRadius == 0) {
                rightTopRadius = radius;
            }
            if (leftBottomRadius == 0) {
                leftBottomRadius = radius;
            }
            if (rightBottomRadius == 0) {
                rightBottomRadius = radius;
            }
        }
        typeArray.recycle()
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        // 保证图片宽高大于圆角宽高， 获取圆角的宽高
        // 取横着大的长度
        val maxLeft = leftTopRadius.coerceAtLeast(leftBottomRadius)
        val maxRight = rightTopRadius.coerceAtLeast(rightBottomRadius)
        val minWidth = maxLeft + maxRight
        // 取竖着大的长度
        val maxTop = leftTopRadius.coerceAtLeast(rightTopRadius)
        val maxBottom = leftBottomRadius.coerceAtLeast(rightBottomRadius)
        val minHeight = maxTop + maxBottom
        if (width > minWidth && height > minHeight) {
            val path = Path()
            //四个角：右上，右下，左下，左上
            path.moveTo(leftTopRadius.toFloat(), 0F)

            path.lineTo((width - rightTopRadius).toFloat(), 0F)
            path.quadTo(width.toFloat(), 0F, width.toFloat(), rightTopRadius.toFloat())

            path.lineTo(width.toFloat(), (height - rightBottomRadius).toFloat())
            path.quadTo(
                width.toFloat(),
                height.toFloat(),
                (width - rightBottomRadius).toFloat(),
                height.toFloat()
            )

            path.lineTo(leftBottomRadius.toFloat(), height.toFloat())
            path.quadTo(0F, height.toFloat(), 0F, (height - leftBottomRadius).toFloat())

            path.lineTo(0F, leftTopRadius.toFloat())
            path.quadTo(0F, 0F, leftTopRadius.toFloat(), 0F)

            canvas!!.clipPath(path)

            /*val paint = Paint()
            paint.setColor(Color.BLUE)
            paint.style = Paint.Style.STROKE
            paint.strokeWidth = 2f
            canvas!!.drawPath(path, paint)*/
        }
        super.onDraw(canvas)
    }
}